package ar.edu.unicen.exa.isistan.archassis.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import ar.edu.unicen.exa.isistan.archassis.DiffRequestData;


public class HTMLResource {
	private static final String RESOURCE_NAME = "veltemp";
	private static final String WORKSPACE_NAME = "html";
	
	public static File getHTMLWorkspace() {
		File tmpDir = DiffRequestData.tempDir();
		File resourceTarget = new File(tmpDir, WORKSPACE_NAME);
		return resourceTarget;
	}
	
	public static File buildLoading(String file) throws FileNotFoundException {
		return export("/loading.html", "/" + file, true);
	}
	
	public static void buildHTMLWorkspace() throws FileNotFoundException {
		//export("onError.html");
		export("/script/jquery-1.6.4.min.js");
		export("/script/jquery-ui-1.8.16.custom.min.js");
		export("/script/jquery-ui-1.8.16.custom.css");
		export("/script/menu.js");
		export("/script/page-uml-diff.js");
		export("/script/page-summary.js");
		export("/script/page-completeClass.js");
		
		export("/css/reset.css");
		export("/css/style.css");
		
		export("/img/print.png");
		export("/img/Class.png");
		export("/img/Interfaz.png");
		export("/img/extends.png");
		export("/img/implements.png");
		export("/img/emptylink.png");
		export("/img/public.png");
		export("/img/protected.png");
		export("/img/private.png");
		export("/img/privateStatic.png");
		export("/img/publicStaticFinal.png");
		export("/img/privateStaticFinal.png");
		export("/img/protectedStaticFinal.png");
		
		export("/script/highcharts.js");
		export("/script/exporting.js");
		export("/img/VolverCuerpo.png");
		export("/img/VolverDerecha.png");
		export("/img/VolverIzquierda.png");
		export("/img/Flecha.png");
		export("/img/FlechaCuerpo.png");
		export("/img/FlechaFinal.png");
		export("/img/flechaUsa.gif");
		export("/img/falta.png");
		export("/img/sobra.png");
		export("/img/flechaUsa.gif");
		export("/img/Loading.gif");
	}
	
	public static void main(String[] args) {
		try {
			HTMLResource.buildHTMLWorkspace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private static void export(String resource) throws FileNotFoundException {
		export(resource, resource, false);
	}
	
	private static File export(String inResource, String outResource, boolean overwrite) throws FileNotFoundException {
		File file = getOutputTargetFile(WORKSPACE_NAME + outResource, overwrite);
		if (file == null) {
			//el archivo existe
			return null;
		}
		OutputStream osFile = new FileOutputStream(file);

		
		InputStream isFile = JarResource.getInstance().getResourceStream(RESOURCE_NAME + inResource);
		copyStream(isFile, osFile);
		return file;
	}
	
	private static void copyStream(InputStream isFile, OutputStream osFile) {
		byte buf[]=new byte[1024];
		int len;
		try {
			while((len=isFile.read(buf))>0) {
				osFile.write(buf,0,len);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try { osFile.close(); } catch (Exception e) {}
			try { isFile.close(); } catch (Exception e) {}
		}
	}
	private static File getOutputTargetFile(String resource, boolean overwrite) throws FileNotFoundException {
		File tmpDir = DiffRequestData.tempDir();
		File resourceTarget = new File(tmpDir, resource);
		if (resourceTarget.isFile()) {
			if (!overwrite) {
				return null; //el archivo existe!
			}
		}
		resourceTarget.getParentFile().mkdirs();
		return resourceTarget;
	}
	
//	private static OutputStream getOutputTarget(String resource) throws FileNotFoundException {
//		
//		OutputStream osFile = new FileOutputStream(resourceTarget);
//		return osFile;
//	}
}
